package com.wuhan.template.module.home.activity;

import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.constant.Urls;
import com.wuhan.template.module.home.adapter.JiaoYuBaseAdapter;
import com.wuhan.template.module.home.adapter.JiaoYuPageAdapter;
import com.wuhan.template.module.home.entity.JiaoYuEntity;
import com.wuhan.template.module.home.listener.MyViewPageListener;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/5/11 0011.
 */
public class JiaoYuActivity extends BaseActivity {

    @BindView(R.id.jiaoyu_viewPage)
    protected ViewPager viewPager;

    @BindView(R.id.jiaoyu_pullListView)
    protected PullToRefreshListView listView;

    @BindView(R.id.jiaoyu_title)
    protected TextView jiaoYuTitle;

    @BindView(R.id.jiaoyu_back_image)
    protected ImageView imageBack;

    @BindView(R.id.jiaoyu_group)
    protected RadioGroup group;

    private int offset=0;
    private Handler handler = new Handler();
    private int i = 0;
    private List<JiaoYuEntity.ListBean> list = new ArrayList<>();
    private JiaoYuBaseAdapter jiaoYuBaseAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_jiaoyu;
    }

    @Override
    protected void initViews() {
        initGroup();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                viewPager.setCurrentItem(++i);
                handler.postDelayed(this, 2500);
            }
        }, 2500);

        imageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(JiaoYuActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        jiaoYuBaseAdapter = new JiaoYuBaseAdapter(this, list);
        listView.setAdapter(jiaoYuBaseAdapter);

        listView.setMode(PullToRefreshBase.Mode.BOTH);

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                listView.getLoadingLayoutProxy().setRefreshingLabel("正在刷新");
                listView.getLoadingLayoutProxy().setPullLabel("下拉刷新");
                listView.getLoadingLayoutProxy().setReleaseLabel("释放开始刷新");

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel("最后更新时间:");
                offset += 0;
                list.clear();
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                listView.getLoadingLayoutProxy().setRefreshingLabel("正在加载");
                listView.getLoadingLayoutProxy().setPullLabel("上拉加载");
                listView.getLoadingLayoutProxy().setReleaseLabel("释放开始加载更多");

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel("最后更新时间:");
                offset += 20;

                loadData();

            }
        });

        loadData();
    }

    private void initGroup() {

        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Intent intent = new Intent();
                switch (checkedId){
                    case R.id.zaixian_shipin:
                        Toast.makeText(JiaoYuActivity.this, "在线视频", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.jiaoyu_huodong:
                        intent.setClass(JiaoYuActivity.this,JiaoYuHuoDong.class);
                        startActivity(intent);
                        break;
                    case R.id.jiaoyu_dianbo:
                       intent.setClass(JiaoYuActivity.this,DianBoActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        });
    }

    @Override
    protected void loadData() {

        OkHttpUtils.get(Urls.JIAOYU+"&offset="+ offset, null);
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {
        if (message.type.equals(Urls.JIAOYU+"&offset="+ offset)) {
            Gson gson = new Gson();
            JiaoYuEntity jiaoYuEntity = gson.fromJson(message.result, JiaoYuEntity.class);
            final List<JiaoYuEntity.SlideBean> slides = jiaoYuEntity.getSlide();
            JiaoYuPageAdapter pageAdapter = new JiaoYuPageAdapter(this, slides);
            viewPager.setAdapter(pageAdapter);
            pageAdapter.notifyDataSetChanged();

            jiaoYuTitle.setText(slides.get(0).getTitle());
            viewPager.addOnPageChangeListener(new MyViewPageListener() {
                @Override
                public void onPageSelected(int position) {
                    i = position;
                    position %= slides.size();
                    if (position < 0) {
                        position = slides.size() + position;
                    }
                    viewPager.setCurrentItem(i);
                    jiaoYuTitle.setText(slides.get(position).getTitle());
                }
            });


            list.addAll(jiaoYuEntity.getList());

            jiaoYuBaseAdapter.notifyDataSetChanged();

            listView.onRefreshComplete();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
